package com.cn.auth.controller.sys;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.auth.common.*;
import com.cn.auth.po.sys.AuthBranch;
import com.cn.auth.po.sys.AuthLog;
import com.cn.auth.po.sys.AuthRole;
import com.cn.auth.po.sys.AuthUser;
import com.cn.auth.service.sys.BranchService;
import com.cn.auth.service.sys.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cn.auth.service.sys.UserService;

import java.util.List;

/**
 * 用户管理Controller
 *
 * @author Maochao-zhu
 */
@Controller
@RequestMapping("/user")
public class UserController {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    UserService userService;
    @Resource
    BranchService branchService;
    @Resource
    RoleService roleService;


    /**
     * 去用户首页
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("goIndex")
    public ModelAndView goIndex(HttpServletRequest request,
                                HttpServletResponse response) {
        ModelAndView modelView = new ModelAndView();
        List<AuthRole> roleList = roleService.getRoleList(null);
        modelView.addObject("roleList", roleList);
        modelView.setViewName("views/user/index");
        return modelView;
    }

    /**
     * 去用户添加界面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("goAdd")
    public ModelAndView goAdd(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView modelView = new ModelAndView();
        //获取部门列表
        List<AuthBranch> branchList = branchService.getBranchList(null);
        //获取角色列表
        List<AuthRole> roleList = roleService.getRoleList(null);
        modelView.addObject("branchList", branchList);
        modelView.addObject("roleList", roleList);
        modelView.setViewName("views/user/add");
        return modelView;
    }

    /**
     * 去编辑页面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("goEdit")
    public ModelAndView goEdit(Integer id, HttpServletRequest request,
                               HttpServletResponse response) {
        ModelAndView modelView = new ModelAndView();
        modelView.addObject("zx_data", userService.getUserById(id));
        //获取部门列表
        List<AuthBranch> branchList = branchService.getBranchList(null);
        //获取角色列表
        List<AuthRole> roleList = roleService.getRoleList(null);
        modelView.addObject("branchList", branchList);
        modelView.addObject("roleList", roleList);

        modelView.setViewName("views/user/edit");
        return modelView;
    }


    /**
     * 获取数据列表信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getList")
    @ResponseBody
    public BasePageData getList(AuthUser user, HttpServletRequest request,
                                HttpServletResponse response) {
        BasePageData data = new BasePageData();
         List<AuthUser> list = userService.getUserList(user);
        Integer count = userService.getUserListCount(user);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        data.setData(list);
        return data;
    }


    /**
     * 保存用户
     *
     * @param user
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("insert")
    @ResponseBody
    public BasePageData insert(AuthUser user, HttpServletRequest request,
                               HttpServletResponse response) {
        BasePageData data = new BasePageData();
        Integer count = userService.getUserByLoginName(user.getUname(),null);
        if(count>0){
            data.setCode(WebResponseCode.FAIL);
            data.setMsg("用户名已存在!");
            return data;
        }
        userService.insertUser(user);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        return data;
    }

    /**
     * 删除操作
     *
     * @param id       字符串id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public BasePageData delete(String id, HttpServletRequest request,
                               HttpServletResponse response) {
        BasePageData data = new BasePageData();
        String[] ids = id.split(",");
        Integer del = userService.deleteUserById(ids);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        return data;
    }


    /**
     * 密码初始化
     *
     * @param id       字符串id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("initpass")
    @ResponseBody
    public BasePageData initpass(String id, HttpServletRequest request,
                                 HttpServletResponse response) {
        BasePageData data = new BasePageData();

        if (!StringUtil.isNotBlank(id)) {
            data.setCode(WebResponseCode.FAIL);
            data.setMsg("用户信息传递错误:" + id);
            return data;
        }
        AuthUser users = UserUtils.getLoginUser();
//        userService.updateUserPass(Integer.valueOf(id), "123456");
//        userService.insertLog(users.getUname() + ",初始化用户[" + userService.getUserNameById(Integer.valueOf(id)) + "]的密码");
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        return data;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public BasePageData update(AuthUser user, HttpServletRequest request,
                               HttpServletResponse response) {
        BasePageData data = new BasePageData();
        Integer count = userService.getUserByLoginName(user.getUname(),user.getId());
        if(count>0){
            data.setCode(WebResponseCode.FAIL);
            data.setMsg("用户名已存在!");
            return data;
        }
        userService.updateUserById(user);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        return data;
    }


    /**
     * 去日志列表界面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("goLogIndex")
    public ModelAndView goLogIndex(HttpServletRequest request,
                                   HttpServletResponse response) {
        ModelAndView modelView = new ModelAndView();
        List<AuthLog> list = userService.getLogList(null);
        Integer count = userService.getLogListCount(null);
        modelView.addObject("list",list);
        modelView.addObject("count",count);

        modelView.setViewName("views/user/log");
        return modelView;
    }

    /**
     * 获取日志列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getLogList")
    @ResponseBody
    public BasePageData getLogList(AuthLog log, HttpServletRequest request,
                                   HttpServletResponse response) {
        BasePageData data = new BasePageData();

        List<AuthLog> list = userService.getLogList(log);
        Integer count = userService.getLogListCount(log);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        data.setData(list);
        return data;
    }

    /**
     * 删除log
     *
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("deleteLog")
    @ResponseBody
    public BasePageData deleteLog(String id, HttpServletRequest request,
                                  HttpServletResponse response) {
        BasePageData data = new BasePageData();
        String[] ids = id.split(",");
        userService.deleteLog(ids);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        return data;
    }

    /**
     * 上传头像
     *
     * @param file
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("uploadImage")
    @ResponseBody
    public BasePageData uploadImage(@RequestParam(value = "file") MultipartFile file,
                                    @RequestParam(required = false, value = "id") String id,
                                    HttpServletRequest request, HttpServletResponse response) {
        BasePageData data = new BasePageData();
        try {
            String filePath = FileUtil.uploadImage("userImage", file, request, response);
            if (null != filePath) {
                data.setCode(WebResponseCode.SUCCESS);
                data.setMsg(filePath);
            } else {
                data.setCode(WebResponseCode.FAIL);
                data.setMsg("上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            data.setCode(WebResponseCode.FAIL);
            data.setMsg("操作失败!");
        }
        return data;
    }

}
